<?php

class AccessFilterController extends Controller
{
	
	public function filters()
	{
		return array(
				'accessControl',
		);
	}
	
	
	public function accessRules()
	{
		return array(
				array('allow',
						'actions'=>array('delete'),
						'roles' => array('admin'),
						//'expression' => function($user, $role){  return 1; }
				),
					array('allow',
							'actions'=>array('create'),
							'users'=>array('@'),
					),
					array('allow',
							'actions'=>array('update'),
							'users'=>array('@'),
					),
					array('allow',
							'actions'=>array('test'),
							'users' => array('*')
							),
					array('deny'),
		);
	}
	
	
	public function actionTest()
	{
		//$this -> layout = '/layouts/main11';
		$auth=Yii::app()->authManager;
		//print_r(crypt('123'));exit;
		//print_r(Yii::app()->user->id);exit;
		//var_dump(Yii::app()->authManager->roles);exit;
		
		# check perm
		//var_dump(Yii::app()->user->checkAccess('deletePost'));
		//exit;
		
		
		
		//$auth->createOperation('updatePost','редактирование записи');
		//exit;
		
		$auth->createOperation('updatePost', 'редактирование записи');
		
		$bizRule='return Yii::app()->user->id == $params["post"]->authID;';
		$task=$auth->createTask('updateOwnPost','редактирование своей записи',$bizRule);
		$task->addChild('updatePost');
		
		$role=$auth->createRole('editor');
		$role->addChild('updatePost');
		
		$auth->assign('reader', 2);
		exit;
		
			
		$this -> render('/accessfilter');
	}
	
	
	public function actionCreate()
	{
		echo 'create';
	}
	
	
	public function actionDelete()
	{
		echo 'delete';
	}
	
	
	public function actionUpdate()
	{
		var_dump(Yii::app()->user->checkAccess('updatePost'));
		echo 'update';
	}
}